#include "../../lib/daytimelib.h"
#include "../servercommon.h"
#include <time.h>

int main() {

    // common code
    int listenfd, connfd;
    struct sockaddr_in servaddr;

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(8989);
    printf("%d\n", ntohs(servaddr.sin_port));
    listenfd = ServerInit(SOCK_STREAM, 0, servaddr);

    // service code
    char buff[MAXLINE];
    time_t ticks;

    struct sockaddr_in clientaddr;
    for ( ; ; ) {
        int len = sizeof(clientaddr);
        connfd = Accept(listenfd, (SA*) &clientaddr, &len);

        printf("%s, %d\n", inet_ntop(AF_INET, &clientaddr, buff, sizeof(buff)), ntohs(clientaddr.sin_port));

        ticks = time(NULL);
        snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
        Write(connfd, buff, strlen(buff));
        Close(connfd);
    }

    Close(listenfd);
    
    return 0;
}